var api = require("../../utils/api.js");
Page({
  data: {
    nameInputValue: "",
    picker: ["居民身份证", "港澳通行证", "台湾居民来往大陆通行证", "护照"],
    pickerIndex: 0,
    certifyCardType: "0",
    idcardInputValue: "",
    phoneInputValue: "",
    codeInputValue: "",
    noteYesText: "获取验证码",
    token: "",
    // 获取验证码按钮是否置灰
    isCanGetCode: true
  },
  onLoad() {
    // 读取缓存数据
    var pickerIndex = wx.getStorageSync('pickerIndex');
    var certifyCardType = wx.getStorageSync('certifyCardType');
    if (!pickerIndex) {
      pickerIndex = 0;
      certifyCardType = "0";
    }
    this.setData({
      nameInputValue: wx.getStorageSync('nameInputValue'),
      pickerIndex: pickerIndex,
      certifyCardType: certifyCardType,
      idcardInputValue: wx.getStorageSync('idcardInputValue'),
      phoneInputValue: wx.getStorageSync('phoneInputValue'),
      codeInputValue: wx.getStorageSync('codeInputValue')
    });
  },
  picker(e) {
    var type = "0";
    if (e.detail.value == 0) {
      type = "0";
    } else if (e.detail.value == 1) {
      type = "B";
    } else if (e.detail.value == 2) {
      type = "C";
    } else {
      type = "Z";
    }
    this.setData({
      pickerIndex: e.detail.value,
      certifyCardType: type,
      codeInputValue: ""
    });
    // 写入缓存并把验证码清空
    wx.setStorageSync('pickerIndex', e.detail.value);
    wx.setStorageSync('certifyCardType', type);
    wx.setStorageSync('codeInputValue', "");
  },
  nameInput(e) {
    this.setData({
      nameInputValue: e.detail.value,
      codeInputValue: ""
    });
    // 写入缓存并把验证码清空
    wx.setStorageSync('nameInputValue', e.detail.value);
    wx.setStorageSync('codeInputValue', "");
  },
  idcardInput(e) {
    this.setData({
      idcardInputValue: e.detail.value,
      codeInputValue: ""
    });
    // 写入缓存并把验证码清空
    wx.setStorageSync('idcardInputValue', e.detail.value);
    wx.setStorageSync('codeInputValue', "");
  },
  phoneInput(e) {
    this.setData({
      phoneInputValue: e.detail.value,
      codeInputValue: ""
    });
    // 写入缓存并把验证码清空
    wx.setStorageSync('phoneInputValue', e.detail.value);
    wx.setStorageSync('codeInputValue', "");
  },
  codeInput(e) {
    this.setData({
      codeInputValue: e.detail.value
    });
    wx.setStorageSync('codeInputValue', e.detail.value);
    // 上次登录已变动
    wx.setStorageSync('logined', false);
  },
  getCode() {
    if (this.data.nameInputValue == null || this.data.nameInputValue == "") {
      wx.showToast({
        title: "姓名不能为空",
        icon: 'none',
        duration: 2000
      });
    } else if (
      this.data.idcardInputValue == null ||
      this.data.idcardInputValue == ""
    ) {
      wx.showToast({
        title: "证件号码不能为空",
        icon: 'none',
        duration: 2000
      });
    } else if (
      this.data.phoneInputValue == null ||
      this.data.phoneInputValue == ""
    ) {
      wx.showToast({
        title: "手机号不能为空",
        icon: 'none',
        duration: 2000
      });
    } else {
      // 开始倒计时
      api.client.countDown(this);

      this.setData({
        codeInputValue: ""
      });
      wx.setStorageSync('codeInputValue', "");

      api.client.getCode(
        function (res) {},
        this.data.nameInputValue,
        this.data.certifyCardType,
        this.data.idcardInputValue,
        this.data.phoneInputValue
      );
    }
  },
  login() {
    if (this.data.nameInputValue == null || this.data.nameInputValue == "") {
      wx.showToast({
        title: "姓名不能为空",
        icon: 'none',
        duration: 2000
      });
      return;
    }
    if (this.data.idcardInputValue == null || this.data.idcardInputValue == "") {
      wx.showToast({
        title: "证件号码不能为空",
        icon: 'none',
        duration: 2000
      });
      return;
    }
    if (this.data.phoneInputValue == null || this.data.phoneInputValue == "") {
      wx.showToast({
        title: "手机号不能为空",
        icon: 'none',
        duration: 2000
      });
      return;
    }
    if (this.data.codeInputValue == null || this.data.codeInputValue == "") {
      wx.showToast({
        title: "验证码不能为空",
        icon: 'none',
        duration: 2000
      });
      return;
    }

    if (wx.getStorageSync('logined')) {
      wx.navigateTo({
        url: "../../pages/main/main"
      });
    } else {
      api.client.login(
        function (res) {},
        this.data.phoneInputValue,
        this.data.codeInputValue,
        this.data.certifyCardType
      );
    }
  }
})